//
//  SEGQueryFeedbackViewController.m
//  ExcellentHome
//
//  Created by 揭婧雯 on 2017/3/9.
//  Copyright © 2017年 陈区. All rights reserved.
//

#import "SEGQueryFeedbackViewController.h"
#import "SEGQueryFeedbackViewModel.h"
#define tipText @"请填写您的要咨询的问题"

@interface SEGQueryFeedbackViewController ()

@end
@interface SEGQueryFeedbackViewController ()
@property(nonatomic, strong) SEGQueryFeedbackViewModel *viewModel;
@end

@implementation SEGQueryFeedbackViewController
@synthesize context = _context;


- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"反馈";
    [self showCustomNavigationBackButton];
    [self showCustomNavigationRightButtonWithTitle:@"提交" image:[UIImage imageWithContentsOfFile:SEGPicName(@"")] hightlightImage:[UIImage imageWithContentsOfFile:SEGPicName(@"")] color:kColor_T];
    
    
    UILabel *type = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, LYScreenWidth(), 43)];
    type.font = [UIFont systemFontOfSize:13];
    type.textColor = kColor_3;
    type.text = @"话费查询";
    [self.view addSubview:type];
    
    UIView* field = [[UIView alloc] initWithFrame:CGRectMake(0, 43, LYScreenWidth(), 125)];
    field.backgroundColor = kColor_W;
    [self.view addSubview:field];
    
    contextTextView = [[UITextView alloc] initWithFrame:CGRectMake(11.5, 5, LYScreenWidth() - 20, 115)];
    contextTextView.font = [UIFont systemFontOfSize:13];
    contextTextView.textColor = kColor_3;
    contextTextView.delegate = self;
    contextTextView.text = tipText;
    contextTextView.backgroundColor = [UIColor clearColor];
    contextTextView.returnKeyType = UIReturnKeyDone;
    contextTextView.keyboardType = UIKeyboardTypeDefault;
    [field addSubview:contextTextView];
}

#pragma mark - 新网络请求
- (void)requestSubmitAdvice
{
    [SVProgressHUD show];
    [self.viewModel requestSuggestsaveWithContext:self.context completed:^(NSString * _Nonnull codeType, NSString * _Nonnull errorMessage) {
        [SVProgressHUD dismiss];
        if ([codeType isEqualToString:@"0"]) {
            self.context = nil;
            
            UIAlertView* alertview = [[UIAlertView alloc] initWithTitle:nil
                                                                message:@"提交成功!"
                                                               delegate:self
                                                      cancelButtonTitle:nil
                                                      otherButtonTitles:@"确认", nil];
            [alertview show];
            alertview.tag = 1;
        }
        else {
            
            UIAlertView* alertview = [[UIAlertView alloc] initWithTitle:nil
                                                                message:errorMessage
                                                               delegate:nil
                                                      cancelButtonTitle:@"确认"
                                                      otherButtonTitles:nil];
            [alertview show];
        }
    }];
}
#pragma mark -
//右侧按钮
- (void)onNavigationRightButtonClicked:(UIButton*)sender
{
    self.context = contextTextView.text;
    
    if ([self.context isEqual:tipText]) {
        [SEGAlertView showTmepMessageWithMessage:tipText];
        return;
    }
    
    if (self.context.length >= 400) {
        [SEGAlertView showTmepMessageWithMessage:@"您输入的反馈过长，请不要超过400字"];
        return;
    }
    
    [contextTextView resignFirstResponder];
    [self.view endEditing:YES];
    
    [self requestSubmitAdvice];
}
- (void)textViewDidBeginEditing:(UITextView*)textView
{
    if ([textView.text isEqual:tipText]) {
        textView.text = @"";
        
        //        textView.textColor = kColor_2;
    }
}
- (void)alertView:(UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
{
    [contextTextView resignFirstResponder];
    contextTextView.delegate = nil;
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - init Views
-(void)segInitViews{
    
}

#pragma mark - RAC Data Binding
- (void)bindViewModel {
}

#pragma mark - getter
-(SEGQueryFeedbackViewModel *)viewModel{
    if (!_viewModel) {
        _viewModel = [[SEGQueryFeedbackViewModel alloc]init];
    }
    return _viewModel;
}
@end
